﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace WinRun
{
    public partial class FormAlias : Form
    {
        private bool newAlias;
        private string currentAlias;
        private string currentCommand;

        public FormAlias()
        {
            InitializeComponent();
        }

        /// <summary>
        /// read in the XML file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormAlias_Load(object sender, EventArgs e)
        {
            LoadAliases();
        }

        /// <summary>
        /// create a new alias
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdNew_Click(object sender, EventArgs e)
        {
            GrpDetails.Enabled = true;
            newAlias = true;
        }

        /// <summary>
        /// Edit an existing alias
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdEdit_Click(object sender, EventArgs e)
        {
            GrpDetails.Enabled = true;
            TxtAlias.Text = currentAlias;
            TxtCommand.Text = currentCommand;
            newAlias = false;
        }

        /// <summary>
        /// this is used to enable/disable the various add/edit/delete buttons
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LvwAliases_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lvw = (ListView)sender;

            if (lvw.SelectedItems.Count > 0)
            {
                CmdEdit.Enabled = true;
                CmdDelete.Enabled = true;
                currentAlias = lvw.SelectedItems[0].Text;
                currentCommand = LvwAliases.SelectedItems[0].SubItems[1].Text;
            }
            else
            {
                CmdEdit.Enabled = false;
                CmdDelete.Enabled = false;
            }
        }

        /// <summary>
        /// cancel any changes and close dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdCancel_Click(object sender, EventArgs e)
        {
            TxtAlias.Text = string.Empty;
            TxtCommand.Text = string.Empty;
            GrpDetails.Enabled = false;
        }

        /// <summary>
        /// Seriously.. what do YOU think it does?
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Applies changes to the aliases.xml file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdUpdate_Click(object sender, EventArgs e)
        {
            Alias a = new Alias(TxtAlias.Text, TxtCommand.Text);

            if (newAlias)
            {
                a.Add();
            }
            else
            {
                a.Change(currentAlias, TxtAlias.Text, TxtCommand.Text);
            }
            LoadAliases();

            TxtAlias.Text = string.Empty;
            TxtCommand.Text = string.Empty;
            GrpDetails.Enabled = false;
        }

        /// <summary>
        /// reads in the aliases.xml file and fills the listview
        /// </summary>
        private void LoadAliases()
        {
            string fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\aliases.xml";

            /** we might not even have a file **/
            if (!File.Exists(fileName))
            {
                /** so we better make one **/
                Alias a = new Alias();
                a.CreateNewAliasFile();
            }
            XElement xml = XElement.Load(fileName);
            string retVal = string.Empty;
            var query = from p in xml.Elements("command")
                        select p;

            /** fill in the listview with alias - command pair **/
            LvwAliases.Items.Clear();
            foreach (var record in query)
            {
                ListViewItem lvi = new ListViewItem(record.Attribute("alias").Value);
                lvi.SubItems.Add(record.Value);
                LvwAliases.Items.Add(lvi);

            }
        }

        /// <summary>
        /// Used to delete an alias
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdDelete_Click(object sender, EventArgs e)
        {
            /** they might be lying, and not really want to delete it **/
            DialogResult res = MessageBox.Show("Are you sure you want to delete " + currentAlias +"?","Confirm Delete",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2 );

            if (res == DialogResult.Yes)
            {
                Alias a = new Alias(currentAlias,currentCommand);
                a.Remove();
                LoadAliases();
            }

        }

        /// <summary>
        /// Used to browse for executables, etc to set the command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdBrowse_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();

            /** initial directoy is "Program Files" **/
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            ofd.Filter = "exe files (*.exe)|*.exe|All files (*.*)|*.*";
            ofd.FilterIndex = 1;

            DialogResult res = ofd.ShowDialog();

            if (res == DialogResult.OK)
            {
                TxtCommand.Text = ofd.FileName;
            }
            
        }


    }
}
